import { defineStore } from 'pinia'
import axios from 'axios'

export interface ServerConfig {
  host: string
  port: number
  username: string
  authType: 'password' | 'privateKey'
  password?: string
  privateKeyPath?: string
  passphrase?: string
}

export const useServerStore = defineStore('server', {
  state: () => ({
    config: null as ServerConfig | null,
    loading: false
  }),

  actions: {
    async getConfig(): Promise<ServerConfig> {
      try {
        const response = await axios.get('/api/server/config')
        this.config = response.data
        return response.data
      } catch (error) {
        console.error('获取服务器配置失败:', error)
        throw error
      }
    },

    async saveConfig(config: ServerConfig): Promise<void> {
      try {
        const response = await axios.post('/api/server/config', config)
        this.config = response.data
      } catch (error) {
        console.error('保存服务器配置失败:', error)
        throw error
      }
    },

    async testConnection(config: ServerConfig): Promise<void> {
      try {
        await axios.post('/api/server/test-connection', config)
      } catch (error) {
        console.error('测试连接失败:', error)
        throw error
      }
    }
  }
})
